using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;

namespace MyLib
{
    public class DAL_MSSQL : DAL
    {
        //DBCommands.EncrypConnectStringInAppConfig(System.Windows.Forms.Application.ExecutablePath + ".config", "/configuration/applicationSettings/MemberAudit.Properties.Settings/setting[@name='ConnectString']/value", Properties.Settings.Default.ConnectString);
        //DBCommands db = new DBCommands(Properties.Settings.Default.ConnectString);

        public string mConnectString = "";

        public DAL_MSSQL(string connectString)
        {
            InitConnectString(connectString);
        }

        #region ConnectString

        //private void InitConnectString(string host, string database, bool integrated_security, string username, string password)
        //{
        //    InitConnectString(ComposeConnectString(host, database, integrated_security, username, password));
        //}

        private void InitConnectString(string connectString)
        {
                mConnectString = connectString;
        }

        private string GetConnectString()
        {
            mConnectString = mConnectString.Replace("Provider=SQLOLEDB;", "");
            return mConnectString;
        }

        public static string ComposeConnectString(string host, string database, bool integrated_security, string username, string password)
        {
            return ComposeConnectString(host, database, integrated_security, username, password, true);
        }

        public static string ComposeConnectString(string host, string database, bool integrated_security, string username, string password, bool provider)
        {
            if (integrated_security)
                return (provider ? "Provider=SQLOLEDB;" : "") + String.Format(@"Data Source={0};Integrated Security=SSPI;Initial Catalog={1}", host, database);
            else
                return (provider ? "Provider=SQLOLEDB;" : "") + String.Format(@"Data Source={0};Initial Catalog={1};User id={2};Password={3}", host, database, username, password);
        }

        #endregion

        public override IDbConnection CreateConnection()
        {
            return new SqlConnection(GetConnectString());
        }

        public override IDbCommand CreateDbCommand()
        {
            return new SqlCommand();
        }

        public override IDbDataAdapter CreateDataAdapter(IDbCommand cmd)
        {
            return new SqlDataAdapter((SqlCommand)cmd);
        }

        public override IDbDataParameter CreateDataParameter()
        {
            return new SqlParameter();
        }

        public override IDbDataParameter CreateDataParameter(string name, object value)
        {
            if ((value is DateTime) && ((DateTime)value== DateTime.MinValue))
                return new SqlParameter(name, DBNull.Value);
            else
                return new SqlParameter(name, value);
        }

        public override string GetParameterPatten()
        {
            return @"((?<!@)@[0-9a-zA-Z_\u4e00-\u9fa5]+)";
        }

    }
}